/*
Copyright (c) 2007,2007 Elizeu Nogueira da R. Jr.

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, 
      this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, 
      this list of conditions and the following disclaimer in the documentation 
      and/or other materials provided with the distribution.
    * Neither the name of the Elizeu Nogueira da R. Jr. nor the names of its 
      contributors may be used to endorse or promote products derived from this 
      software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


This package is based on HypersonicSQL, originally developed by Thomas Mueller.
and HSQLDB original developed by the HSQL Development Group
*/

package br.com.dreamsource.mobile.jmesql;
import br.com.dreamsource.mobile.jmesql.exceptions.*;

public class jdbcStatement {
    private jdbcConnection cConnection;
    private jdbcResultSet  rSet;
    private boolean	   bEscapeProcessing;
    private int		   iMaxRows;

    public jdbcResultSet executeQuery(String sql) throws SQLException {
	fetchResult(sql);

	return rSet;
    }

    public int executeUpdate(String sql) throws SQLException {
	fetchResult(sql);

	if (rSet == null) {
	    return -1;
	}

	return rSet.getUpdateCount();
    }

    public void close() {
	if (Trace.TRACE) {
	    Trace.trace();
	}

	closeOldResult();

	rSet = null;
    }

    public int getMaxFieldSize() {
	if (Trace.TRACE) {
	    Trace.trace();

	    // todo
	}

	return 0;
    }

    public void setMaxFieldSize(int max) {
	if (Trace.TRACE) {
	    Trace.trace();

	    // todo
	}
    }

    public int getMaxRows() {
	if (Trace.TRACE) {
	    Trace.trace();
	}

	return iMaxRows;
    }

    public void setMaxRows(int max) {
	if (Trace.TRACE) {
	    Trace.trace();
	}

	iMaxRows = max;
    }

    public void setEscapeProcessing(boolean enable) {
	if (Trace.TRACE) {
	    Trace.trace();
	}

	bEscapeProcessing = enable;
    }

    public int getQueryTimeout() {
	if (Trace.TRACE) {
	    Trace.trace();

	    // todo
	}

	return 0;
    }

    public void setQueryTimeout(int seconds) {
	if (Trace.TRACE) {
	    Trace.trace();

	    // todo
	}
    }

    public void cancel() {
	if (Trace.TRACE) {
	    Trace.trace();
	}
    }

    public void clearWarnings() {
	if (Trace.TRACE) {
	    Trace.trace();
	}
    }

    public void setCursorName(String name) {
	if (Trace.TRACE) {
	    Trace.trace();
	}
    }

    public boolean execute(String sql) throws SQLException {
	if (Trace.TRACE) {
	    Trace.trace();
	}

	fetchResult(sql);

	if (rSet == null) {
	    return false;
	}

	return rSet.isResult();
    }

    public jdbcResultSet getResultSet() {
	if (Trace.TRACE) {
	    Trace.trace();
	}

    if (rSet != null && rSet.isResult())
      return rSet;
    return null;
    }

    public int getUpdateCount() {
	if (rSet == null) {
	    return -1;
	}

	return rSet.getUpdateCount();
    }

    public boolean getMoreResults() {
	if (Trace.TRACE) {
	    Trace.trace();
	}
	if (rSet != null) {
		rSet.close();
		rSet = null;
	}

	return false;
    }

    public void setFetchDirection(int direction) throws SQLException {
	if (Trace.TRACE) {
	    Trace.trace();
	}

	if (direction != jdbcResultSet.FETCH_FORWARD) {
	    throw Trace.error(Trace.FUNCTION_NOT_SUPPORTED);
	}
    }

    public int getFetchDirection() {
	if (Trace.TRACE) {
	    Trace.trace();
	}

	return jdbcResultSet.FETCH_FORWARD;
    }

    public void setFetchSize(int rows) {
	if (Trace.TRACE) {
	    Trace.trace();
	}
    }

    public int getFetchSize() {
	if (Trace.TRACE) {
	    Trace.trace();
	}

	return 1;
    }

    public int getResultSetConcurrency() {
	if (Trace.TRACE) {
	    Trace.trace();
	}

	return jdbcResultSet.CONCUR_READ_ONLY;
    }

    public int getResultSetType() {
	if (Trace.TRACE) {
	    Trace.trace();
	}

	return jdbcResultSet.TYPE_FORWARD_ONLY;
    }

    public void addBatch(String sql) throws SQLException {
	if (Trace.TRACE) {
	    Trace.trace();

	    // todo
	}

	throw Trace.error(Trace.FUNCTION_NOT_SUPPORTED);
    }

    public void clearBatch() throws SQLException {
	if (Trace.TRACE) {
	    Trace.trace();
	}

	throw Trace.error(Trace.FUNCTION_NOT_SUPPORTED);
    }

    public int[] executeBatch() throws SQLException {
	if (Trace.TRACE) {
	    Trace.trace();

	    // todo
	}

	throw Trace.error(Trace.FUNCTION_NOT_SUPPORTED);
    }

    public jdbcConnection getConnection() {
	if (Trace.TRACE) {
	    Trace.trace();
	}

	return cConnection;
    }

    jdbcStatement(jdbcConnection c) {
	bEscapeProcessing = true;
	cConnection = c;
    }

    private void fetchResult(String sql) throws SQLException {
	if (bEscapeProcessing) {
	    sql = cConnection.nativeSQL(sql);
	}

	closeOldResult();

	if (iMaxRows == 0) {
	    rSet = cConnection.execute(sql);
	} else {
	    try {
		sql = "SET MAXROWS " + iMaxRows + ";" + sql;
		rSet = cConnection.execute(sql);

		cConnection.execute("SET MAXROWS 0");
	    } catch (SQLException e) {
		cConnection.execute("SET MAXROWS 0");

		throw e;
	    }
	}
    }

    private void closeOldResult() {

	// this is necessary to conform the JDBC standard
	if (rSet != null) {
	    rSet.close();

	    rSet = null;
	}
    }

}
